import logging
import sys

from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox

from src.ui import (
    main_ui_inst,
    setup_main_ui,
)
from src.utils.load_config import check_config, load_config
from src.utils.resources_path import resource_path
from src.utils.setup_log import setup_logger

ICON = resource_path("src/hit.ico")

# 配置日志
setup_logger()

logger = logging.getLogger("src.main")


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

    def check_config(self):
        self.config = load_config()
        failed_msg = check_config(self.config)
        if failed_msg:
            QMessageBox.critical(
                self, "配置错误", "\n".join(failed_msg), QMessageBox.StandardButton.Ok
            )

        self.ui = main_ui_inst
        self.setWindowIcon(QIcon(ICON))
        setup_main_ui(self.ui, self.config, self)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    window.check_config()
    sys.exit(app.exec())
